#ifndef _BPMDetection_CPP_
#define _BPMDetection_CPP_

#include <fmod.h>
#include <bpm_detect.h>
#include "BPMDetection.h"

FMOD_RESULT F_CALLBACK dspCallback(FMOD_DSP_STATE *dsp_state, float *inbuffer, float *outbuffer, unsigned int length, int inchannels, int outchannels)
{
    void *bpmDetection;
    FMOD_DSP *thisdsp = dsp_state->instance;

    FMOD_DSP_GetUserData(thisdsp, &bpmDetection);

    /*
        This loop assumes inchannels = outchannels, which it will be if the DSP is created with '0'
        as the number of channels in FMOD_DSP_DESCRIPTION.
        Specifying an actual channel count will mean you have to take care of any number of channels coming in,
        but outputting the number of channels specified.  Generally it is best to keep the channel
        count at 0 for maximum compatibility.
    */
    for (unsigned int count = 0; count < length; count++)
    {
        for (int count2 = 0; count2 < outchannels; count2++)
        {
            outbuffer[(count * outchannels) + count2] = inbuffer[(count * inchannels) + count2];
            if (count2 == 0) {
                BPM_AddSample(bpmDetection, inbuffer[(count * inchannels) + count2] * 32767.0f);
            }
        }
    }

    return FMOD_OK;
}

#endif
